/*
 * Created on Aug 26, 2011
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package demo.basic.employeedb.client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

import org.systinet.wasp.webservice.LookupException;
import org.systinet.wasp.webservice.Registry;

import demo.basic.employeedb.core.DBException;
import demo.basic.employeedb.core.Employee;
import demo.basic.employeedb.core.EmployeeDB;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class EmployeeDBClient {
    private static String serviceWSDLPath = "/demo/basic/EmployeeDB/wsdl";
    private static final int ADD_ACTION = 1;
    private static final int REMOVE_ACTION = 2;
    private static final int GET_ACTION = 3;
    private static final int LISTALL_ACTION = 4;
    private static final int QUIT_ACTION = 5;

    public static void main(String[] args) {

        String serverURL = System.getProperty("systinet.demo.server.url", "http://localhost:6060");

        // lookup of EmployeeDBService
        EmployeeDB employeeDBService = null;
		try {
			employeeDBService = (EmployeeDB)Registry.lookup(serverURL + serviceWSDLPath,
					EmployeeDB.class);
		} catch (LookupException e) {
			System.out.println("ERROR: Could not find the service");
			System.out.println("System shutdown");
			System.exit(1);
		}

		int action = LISTALL_ACTION;
		boolean nonStop = true;
        System.out.println("Welcome to EmployeeDB service!");
        System.out.println("There are several options for you:");
        System.out.println("\t1: Add Employee");
        System.out.println("\t2: Remove Employee");
        System.out.println("\t3: Get Employee");
        System.out.println("\t4: ListAll Employee");
        System.out.println("\t5: Quit");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int id;
        String name;
        while(nonStop) {
        	try {
	            System.out.print("Choose your option (among 1,2,3,4,5): ");
	            action = Integer.parseInt(bufferedReader.readLine());
	            System.out.println("\tAction: "+action);
	            switch(action) {
	            case ADD_ACTION:
	            	System.out.print("Employee's ID: ");
	            	id = Integer.parseInt(bufferedReader.readLine());
	            	System.out.print("Employee's Name: ");
	            	name = bufferedReader.readLine();
	            	try {
						employeeDBService.add(new Employee(id,name));
					} catch (DBException e1) {
						System.out.println("ERROR: Could not add the employee");
						e1.printStackTrace();
					}
	            	break;
	        	case REMOVE_ACTION:
	        		System.out.print("Employee's ID: ");
	        		id = Integer.parseInt(bufferedReader.readLine());
	            	try {
						employeeDBService.remove(id);
					} catch (DBException e1) {
						System.out.println("ERROR: Could not remove the employee");
						e1.printStackTrace();
					}
	        		break;
	            case GET_ACTION:
	        		System.out.print("Employee's ID: ");
	        		id = Integer.parseInt(bufferedReader.readLine());
	            	try {
						Employee e = employeeDBService.get(id);
						System.out.println("Employee ID: "+e.getId()+" Name: "+e.getName());
					} catch (DBException e1) {
						System.out.println("ERROR: Could not get the employee");
						e1.printStackTrace();
					}
	        		break;
	        	case LISTALL_ACTION:
	        		List<Employee> employees = employeeDBService.getAll();
	        		if(employees.isEmpty()) {
	        			System.out.println("Employee DB is empty");
	        		} else {
	        			for(int i=0;i<employees.size();i++) {
	        				Employee e = (Employee)employees.get(i);
	        				System.out.println("Employee ID: "+e.getId()+" Name: "+e.getName());
	        			}
	        		}
	        		break;
	        	case QUIT_ACTION:
	        		System.out.println("Quit service. Bye bye!");
	        		nonStop = false;
	        		break;
	        	default:
	        		System.out.println("Wrong option, please choose the right one!!!");
	        		break;
	            }
        	}catch(Exception exp) {
        		System.out.println("Wrong option, please choose the right one!!!");
        		action = LISTALL_ACTION;
        	}
        }

    }
}
